//
//  MyCollecgViewController.m
//  项目A --半糖
//
//  Created by romana on 16/6/2.
//  Copyright © 2016年 romana. All rights reserved.
//

#import "MyCollecgViewController.h"
#import "DataBaseManager.h"
#import "UIImageView+WebCache.h"
#import "SecondTabHotModel.h"

#import "GiftCollectionViewCell.h"
#import "GiftNullCollectionViewCell.h"
#import "SecondTabHotDetailsViewController.h"


#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height

@interface MyCollecgViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>

@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSMutableArray *dataArr;
@property (nonatomic, strong) UITableView *tableView;


@end

@implementation MyCollecgViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"我的收藏";
    
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:218 / 255.0 green:82 / 255.0 blue:146 / 255.0 alpha:1];
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 40)];
    titleLabel.backgroundColor = [UIColor clearColor];
    titleLabel.font = [UIFont systemFontOfSize:20];
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.text = @"我的礼物";
    self.navigationItem.titleView = titleLabel;
    
    
    [[DataBaseManager shareIndtance] openHotDB];
    [self dataHandle];
    [self createCollectionView];
    
    // 获取路径
    self.path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)firstObject];
    

    
    // Do any additional setup after loading the view.
}

-(void)dataHandle{
    
    self.dataArr = [[DataBaseManager shareIndtance] selectAllHot];
}
-(void)layoutSublayersOfLayer:(CALayer *)layer{
    [super layoutSublayersOfLayer:layer];
    [self.tableView reloadData];
}

-(void)createCollectionView{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumInteritemSpacing = 10;
    layout.minimumLineSpacing = 10;
    layout.itemSize = CGSizeMake((WIDTH - 20 - 10) / 2, (HEIGHT - 64 - 49 - 30) / 2 - 10);
    layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    self.collectionView = [[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:layout];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:_collectionView];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    [self.collectionView registerClass:[GiftCollectionViewCell class] forCellWithReuseIdentifier:@"collectionCell"];
    [self.collectionView registerClass:[GiftNullCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if (0 == self.dataArr.count) {
        return 1;
    }else{
        return self.dataArr.count;
    }
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    if (0 == self.dataArr.count) {
        GiftNullCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
        cell.lable.text = @"暂无收藏 请前去收藏";
        return cell;
    }else {
        GiftCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"collectionCell" forIndexPath:indexPath];
        cell.backgroundColor = [UIColor whiteColor];
        SecondTabHotModel *h = self.dataArr[indexPath.row];
        
        cell.nameLable.text = h.name;
        [cell.nameLable sizeToFit];
        NSString *path = [self.path stringByAppendingPathComponent:h.cover_image_url];
        cell.imgView.image = [UIImage imageWithContentsOfFile:path];
        return cell;
    }
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    if (self.dataArr.count) {
        SecondTabHotDetailsViewController *detailVC = [[SecondTabHotDetailsViewController alloc] init] ;
        SecondTabHotModel *h = self.dataArr[indexPath.row];
        detailVC.myUrl = h.url;
        detailVC.type = h.type;
        detailVC.nameStr = h.title;
        [self.navigationController pushViewController:detailVC animated:YES];
    }
}
-(void)viewWillAppear:(BOOL)animated{
    self.dataArr = [[DataBaseManager shareIndtance] selectAllHot];
    [self.collectionView reloadData];
}






@end
